/**
 * Created by Administrator on 2016/12/23.
 */

$(function(){
    /*解析href*/
    var str = location.search;
    str = str.slice(1).split('&');
    var json = {};
    for (var i = 0; i < str.length; i++) {
        var a = str[i].split('=');
        json[a[0]] = a[1];
    }
    //console.log(json);

    var
        oReceptionAt        = $('#receptionAt'),//
        oCategoryName       = $('#categoryName'),//
        oAttribute          = $('#attribute'),//规格
        oMerchandiseUnit    = $('#merchandiseUnit'),//
        oActualPrice        = $('#actualPrice'),//
        oQuantity           = $('#quantity'),//工地数量
        oAdjustQuantity     = $('#adjustQuantity'),//
        oAdjustReason       = $('#adjustReason'),//
        oAdjustQuantity2    = $('#adjustQuantity2'),//调整后数量
        oTotalPrice         = $('#totalPrice');//

    var
        oReceptionAtVal = '',
        oCategoryNameVal = '',
        oAttributeVal = '',
        oMerchandiseUnitVal = '',
        oActualPriceVal ='',
        oQuantityVal = '',
        oAdjustQuantityVal = '',
        oAdjustReasonVal = '',
        oAdjustQuantity2Val = '',
        oTotalPriceVal = '';

    if(json.edit == 1){
        oReceptionAt.val(json.receptionAt);
        oCategoryName.val(json.categoryName);
        oAttribute.val(json.attribute);
        oMerchandiseUnit.val(json.merchandiseUnit);
        oActualPrice.val(json.actualPrice);
        oQuantity.val(json.quantity);
        oAdjustQuantity.val(json.adjustQuantity);
        oAdjustReason.val(json.adjustReason);
        oAdjustQuantity2.val(json.adjustQuantity2);
        oTotalPrice.val(json.totalPrice);
    }

    $('#saveBtn').click(function(){
        oReceptionAtVal     = oReceptionAt.val(),
            oCategoryNameVal    = oCategoryName.val(),
            oAttributeVal       = oAttribute.val(),
            oMerchandiseUnitVal = oMerchandiseUnit.val(),
            oActualPriceVal     =oActualPrice.val(),
            oQuantityVal        = oQuantity.val(),
            oAdjustQuantityVal  = oAdjustQuantity.val(),
            oAdjustReasonVal    = oAdjustReason.val(),
            oAdjustQuantity2Val = oAdjustQuantity2.val(),
            oTotalPriceVal      = oTotalPrice.val();

        if(json.edit == 1){
            $.ajax({
                url : `http://192.168.59.3:8082/console/shopping/platformMatssubUp?id=${json.id}&adjustQuantity=${adjustQuantity}&adjustReason=${adjustReason}`,
                type : 'put',
                success : function (){}
            });
        }else{
            //console.log('add???哈哈哈哈');
            //$.post(`http://192.168.59.3:8082/console/shopping/platformMatssubAdd?userCode=${1}&teamId=${1}&projectCode=${json.projectCode}&categoryId=${json.categoryId}&merchandiseId=${1}&merchandiseUnit=${merchandiseUnit}&actualPrice=${actualPrice}&shopId=${json.shopId}&mpId=${1}&quantity=${quantity}&adjustQuantity=${adjustQuantity}&adjustReason=${adjustReason}&receptionAt=${oReceptionAtVal}`
            $.post(`http://192.168.59.3:8082/console/shopping/purMatsubAdd?userCode=${json.userCode}&teamId=${json.teamId}&projectCode=${json.projectCode}&categoryId=${json.categoryId}&merchandiseId=${json.merchandiseId}&merchandiseUnit=${merchandiseUnit}&actualPrice=${actualPrice}&shopId=${json.shopId}&mpId=${1}&quantity=${quantity}&adjustQuantity=${adjustQuantity}&adjustReason=${adjustReason}&receptionAt=${oReceptionAtVal}`
                ,function () {
                    console.log("成功回调");
                    parent.layer.closeAll();
                },json);
            /*            $.ajax({
             type : 'POST',
             data: JSON.stringify(oData),
             url : `http://192.168.59.3:8082/console/shopping/platformMatssubAdd?userCode=1&teamId=1&projectCode=GC335324X5I2&categoryId=1&merchandiseId=1&merchandiseUnit=1&actualPrice=1&shopId=1&mpId=1&quantity=2&adjustQuantity=1&adjustReason=%E5%A2%9E%E5%8A%A0&receptionAt=${oReceptionAtVal}`,
             dataType : 'json',
             contentType : 'application/json',

             success : function (data){
             parent.layer.closeAll();
             },
             error : function (err){
             console.log( "错误");
             }
             })*/
        }
    });
});

function responseHandler(res) {
    var needData = {
        rows: res.data.list,
        total: res.data.total
    };
    return needData;
}
